<?php


/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

class HelpDeskHelperManager 
{
	/**
	 * 
	 * @param $id
	 * @return unknown_type
	 */
	function isUser( $userid )
	{
		// is this user a manager?
		$success = false;
		
		$userid = intval($userid);
		if (!$userid) {
			return $success;
		}
		
		$user = HelpDeskConfig::getUser( $userid );
		if ( $user->gid == '25' ) {
			$success = true;
		}
		
		$cats = HelpDeskHelperManager::getCategories( $userid );
		if ($cats && is_array($cats)) {
			$success = true;
		}
		
		return $success;
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function getCategories( $userid )
	{
        static $instance;
        
        if (!isset($instance[$userid]))
        {
        	if (!is_array($instance)) { $instance = array(); }
        	
        	$user = HelpDeskConfig::getUser( $userid );
        	if ($user->gid == '25') {
		        $instance[$userid] = HelpDeskHelperCategory::getAll();
		        return $instance[$userid];
			}
			
	        $instance[$userid] = array();
	        $done = array();
	        
        	$database = HelpDeskConfig::getDBO();	
        	$cat_query = " AND db.userid = '".$userid."' ";
	        $query = "
	            SELECT
	                db.*, c.*
	            FROM
	                #__helpdesk_u2c AS db
	            LEFT JOIN
	                #__helpdesk_categories AS c ON db.categoryid = c.id
	            WHERE 1
	                {$cat_query}
	        ";
	
	        $database->setQuery( $query );
	        $data = $database->loadObjectList();
	        
	        for ($i=0; $i<count($data); $i++) {
	        	$d = $data[$i];
	        	if (!isset($done[$d->id])) {
		        	$instance[$userid][] = $d;
					$children = HelpDeskHelperCategory::getChildren( $d->id, '1' );
					for ($c=0; $c<count($children); $c++) {
						$child = $children[$c];
						if (!isset($done[$child->id])) {
							$instance[$userid][] = $child;		
							$done[$child->id] = '1';		
						}
					}
					$done[$d->id] = '1';
	        	}
	        }
        }

        return $instance[$userid];
	}

	/**
	 * 
	 * @param $userid
	 * @param $catid
	 * @param $returnObject
	 * @return unknown_type
	 */
	function isCategory( $userid, $catid, $returnObject='0' ) 
	{
        $success = false;
        $database = HelpDeskConfig::getDBO();
        HelpDeskHelperUtility::import('joomla.utilities.arrayhelper'); //joomla/utilities/arrayhelper.php (line 23)
        $cats = HelpDeskHelperManager::getCategories( $userid );
        $catids = HelpDeskArrayHelper::getColumn( $cats, 'id' ); 

        if (is_array( $catids ) && in_array( $catid, $catids )) {
	        $success = true;
            if ($returnObject == '1') {
                $success = HelpDeskHelperCategory::getInstance( $catid );
            }
        }

        return $success;
	}
	
	/**
	 * 
	 * @param $userid
	 * @param $catid
	 * @return unknown_type
	 */
	function addCategory( $userid, $catid )
	{
		$success = false;
		$database = HelpDeskConfig::getDBO();
		
	  	$query = "
	  		INSERT INTO 
	  			#__helpdesk_u2c
			SET 
				`userid` = '{$userid}', 
				`categoryid` = '{$catid}' 
		";
		$database->setQuery( $query );
		if ($database->query()) 
		{ 
			$success = true; 
		}
		
		return $success;
	}
	
	/**
	 * 
	 * @param $userid
	 * @param $catid
	 * @return unknown_type
	 */
	function removeCategory( $userid, $catid )
	{
		$success = false;
		$database = HelpDeskConfig::getDBO();
		
		$query = "
			DELETE FROM 
				#__helpdesk_u2c
			WHERE 
				`userid` = '{$userid}'
			AND 
				`categoryid` = '{$catid}'
		";
		$database->setQuery( $query );
		if ($database->query()) 
		{ 
			$success = true; 
		}
		
		return $success;		
	}
}